package drawEngine;

import iterfaces.Ships;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import Chat.A_Chat_Client;
import Chat.A_Chat_Server;

import com.sun.java.swing.plaf.windows.resources.windows;
import com.sun.jmx.snmp.tasks.Task;

import controllers.Config;
import controllers.DataInsertion;
import controllers.Map;
import controllers.Point;
import controllers.Waypoint;

import ships.Destroyer;
import ships.Ship;
import sun.misc.Regexp;
import weapons.Bullet;
import weapons.Canonball;
import weapons.Missile;
import weapons.Torpedo;
import windows.CreateNewGame;
import windows.GameConsole;
import windows.GameLobby;

public class DrawPanel extends JPanel implements MouseListener, MouseWheelListener {

	private static int DELAY = 100;
	private static final long serialVersionUID = 1L;
	boolean moving = false;
	private Timer timer;
	
	public DrawPanel(){
		addMouseListener(this);
		this.setBackground(Color.blue);
		Config.getConfig().scale = 376;
		painter();
	}
	
	@Override 
	public void paintComponent(Graphics g) {
         super.paintComponent(g);    // paints background
         if(Map.getMap().sObjects != null) {
	         Iterator<Ships> it = Map.getMap().sObjects.iterator();
	         
	         while(it.hasNext()){
	         	 Ships o = it.next();
	         	 
	        	 Image img=null;
	
	        	 try {
	            	 img=ImageIO.read(new File("images" + o.getImage()));
	             } catch(IOException e){
	            	 
	             }
	        	 
	        	 g.drawImage(img, (int) o.getPosition().get().getX() - (img.getWidth(null)/2), (int) o.getPosition().get().getY() - (img.getHeight(null)/2), this);
	        	 it.remove();
	         }
         }
         if(Map.getMap().dObjects != null) {
	         Iterator<Ships> it = Map.getMap().dObjects.iterator();
	         
	         while(it.hasNext()){
	         	 Ships o = it.next();
	         	 
	         	 if(Config.getConfig().chatClient != null && Config.getConfig().chatClient.hasConnected){
	         		 if(!o.getClass().equals(Waypoint.class)){
	         			 if(o.getCurrentWayPoint() != null){
	         				 Config.getConfig().chatClient.SENDGAMEDATA(Config.getConfig().nickname + ":" + GameLobby.groupName + ":" + o.getImage() + "|"+ o.getClass() +":" + o.getPosition().get().getX() + ";" + o.getPosition().get().getY() + ":" + o.getMaximumSpeed() + ":(" + o.getCurrentWayPoint().getDirection().get().getX() + ";" + o.getCurrentWayPoint().getDirection().get().getY() + ")");
	         			 }
	         		 }
	         	 }
	         	 
	         	 // Ship object / Waypoint must be deleted.
	        	 if(o.getDelete()){
	        		 it.remove();
	        	 }
	         	 
	        	 Image img = null;
	
	        	 try {
	            	 img=ImageIO.read(new File("images" + o.getImage()));
	             } catch(IOException e){
	            	 
	             }
	        	 
	        	 boolean makeNextStep = true;
	        	 
	        	 // Object staat stil, en er word een nieuw waypoint gezet.
	        	 if(o.getCurrentWayPoint() == null){
	        		 // System.out.println("Geen waypoint nu");
	        		 if(o.hasNextWayPoint()){
	        			 o.nextWaypoint();
	        			 // System.out.println("Wel een nieuwe waypoint");
	        		 } else {
	        			 // Bullet heeft einde bereikt
	        			 if(o.getClass().equals(Missile.class) || o.getClass().equals(Torpedo.class) || o.getClass().equals(Canonball.class)|| o.getClass().equals(Canonball.class)){
	        				 o.deleteObject(true);
	        			 }
	        		 }
	        	 }
	        		 
	        	 if(o.getCurrentWayPoint() != null){
	        		 if(o.getCurrentWayPoint().reached()){
	        			 // Delete current waypoint for display
	        			 o.getCurrentWayPoint().deleteObject(true);
	        			 o.setLastWaypoint(o.getLastWaypoint() + 1);
	        			 o.nextWaypoint();
	        			 if(o.getCurrentWayPoint() == null){
	        				 makeNextStep = false;
	        			 }
	        		 }
	        		
	        		 if(o.getDraw()){
		        		 if(makeNextStep){
		        			Point nextPos = o.getCurrentWayPoint().nextPosition();
		        		 	if(o.getClass().equals(Waypoint.class))
		        		 		g.drawImage(img, (int) nextPos.getX() - (img.getWidth(null)/2), (int) nextPos.getY() - (img.getHeight(null)/2), this);
		        		 	o.setPosition(nextPos);
		        		 } else {
		        			 g.drawImage(img, (int) o.getPosition().get().getX() - (img.getWidth(null)/2), (int) o.getPosition().get().getY() - (img.getHeight(null)/2), this);        		 
		        			 o.setPosition(new Point(o.getPosition().get().getX(), o.getPosition().get().getY()));
		        		 }
	        		 }
	        	 } else {
	        		 if(o.getDraw()){
	        			 if(o.getClass().equals(Waypoint.class))
	        				 g.drawImage(img, (int) o.getPosition().get().getX() - (img.getWidth(null)/2), (int) o.getPosition().get().getY() - (img.getHeight(null)/2), this);        		 
	        			 o.setPosition(new Point(o.getPosition().get().getX(), o.getPosition().get().getY()));
	        		 }
	        	 }
	         }
         }
    }
	
	public void painter(){
		if(timer == null){
			System.out.println("Painter running!");
			timer = new Timer();
			TimerTask t = new TimerTask() {			
				@Override
				public void run() {
					repaint();
					revalidate();
				}
			};
			timer.schedule(t, 0, DELAY);
		}
	}
	

	
	
	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {	
		if(e.getButton() == 1){
			// LEFT MOUSE
			Map.getMap().dObjects.get(0).addWaypoint(Map.getMap().dObjects.get(0), new Point(e.getX(), e.getY()));
			painter();
			
			//DataInsertion.LogInsert(GameConsole.UserName.toString(), String.format("Waypoint:  X %i  Y %i", e.getX(), e.getY()));
			String wpX = ""+e.getX();
			String wpY = ""+e.getY();
			DataInsertion.LogInsert(GameConsole.UserName.toString(), "Waypoint: " + "X" + wpX + " Y" + wpY);
		} else if(e.getButton() == 3){
			// RIGTH MOUSE
			System.out.println("fire!");
			Map.getMap().dObjects.get(0).shoot(Map.getMap().dObjects.get(0).getPosition(), new Point(e.getX(), e.getY()), Config.getConfig().selectedWeapon);
		}
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
